﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xlit.Example.Business.Services;
using Xlit.Project.Entity.Domain;

namespace Xlit.Example.Controllers
{
    /// <summary>
    ///  角色管理
    /// </summary>
    [Route("api/role")]
    public class SysRoleController : Xlit.Project.Mvc.Controller.ControllerBase
    {
        /// <summary>
        ///  角色管理
        /// </summary>
        public SysRoleService _SysRoleService { get; set; }
        /// <summary>
        ///  菜单管理
        /// </summary>
        public SysMenuService _SysMenuService { get; set; }

        /// <summary>
        ///  获取角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get()
        {
            var output =  _SysRoleService.Get();
            return Success(data: output);
        }

        /// <summary>
        ///  获取角色
        /// </summary>
        /// <param name="Id">角色Id</param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public IActionResult Get(int Id)
        {
            var output =  _SysRoleService.Get(Id);
            return Success(data: output);
        }

        /// <summary>
        ///  新增/更新角色
        /// </summary>
        /// <param name="input">角色</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Post([FromBody] Sys_Role input)
        {
             _SysRoleService.Update(input);
            return Success();
        }

        /// <summary>
        ///  删除角色
        /// </summary>
        /// <param name="Id">角色id</param>
        /// <returns></returns>
        [HttpDelete("{Id}")]
        public IActionResult Delete(int Id)
        {
             _SysRoleService.Delete(Id);
            return Success();
        }

        /// <summary>
        ///  获取角色的操作权限
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}/permission")]
        public IActionResult ActionPermission(int Id)
        {
            return Success(data: new
            {
                Menu =  _SysMenuService.Get(),
                Checked =  _SysRoleService.GetMenuPermission(Id)
            });
        }

        /// <summary>
        ///  获取角色的菜单权限
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}/menu")]
        public IActionResult GetMenuPermission(int Id)
        {
            var output = new
            {
                Menu =  _SysMenuService.Get(),
                Checked =  _SysRoleService.GetMenuPermission(Id)
            };
            return Success(data: output);
        }

        /// <summary>
        ///  获取角色的菜单权限
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="checkedId"></param>
        /// <returns></returns>
        [HttpPost("{Id}/menu")]
        public IActionResult SaveMenuPermission(int Id, [FromBody] IList<int> checkedId)
        {
             _SysRoleService.SaveMenuPermission(Id, checkedId);
            return Success();
        }
    }
}